package com.dbq.fast.business.product.api;

import android.app.Activity;

import com.dbq.fast.App;
import com.dbq.fast.business.product.bean.ProductType;
import com.dbq.fast.business.product.bean.ServiceType;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.net.LogHttpManager;
import com.dbq.fast.util.SystemUtil;
import com.yiba.vpn.core.LocalVpnService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class EventApi {

    public static void send2DdEvent(String type, String status,//
                                    int resultCode, String result,//
                                    String level, ProductType.SetMealListBean checkProduct) {
        HashMap<String, String> param = new HashMap<>();
        param.put("type", type);
        param.put("status", status);
        param.put("resultCode", String.valueOf(resultCode));
        param.put("result", result);
        param.put("country", SystemUtil.getCountry(App.getContext()));
        param.put("vpnId", User.getInstance().getVpnId());
        param.put("version", SystemUtil.getAppVersionName(App.getContext()));
        param.put("vpnConnectStatus", String.valueOf(LocalVpnService.IsRunning));
        if (checkProduct != null) {
            param.put("DollarPrice", checkProduct.setMealDiscountDollarPrice);
        }
        param.put("level", String.valueOf(level));
        LogHttpManager.getInstance()
                .getHttpService()
                .paymentInfo(param)
                .compose(new CommonTransformer<>())
                .subscribe(httpResult -> {
                }, throwable -> {
                });
    }

    public static void saveEvent(Activity activity, String type, String segment,//
                                 ServiceType.AppServiceListBean chooseServiceExtra) {
        HashMap<String, String> param = new HashMap<>();
        param.put("event", type);
        Date date = new Date();
        param.put("timestamp", String.valueOf(date.getTime()));
        param.put("timePhone", getTime(date));
        param.put("sdkVersion", android.os.Build.VERSION.SDK);
        param.put("uuid", UUID.randomUUID().toString());
        param.put("androidid", SystemUtil.getAndroidId(activity));
        param.put("phoneType", android.os.Build.MODEL);
        param.put("language", SystemUtil.getLanguage(activity));
        param.put("country", SystemUtil.getCountry(activity));
        param.put("appVersion", SystemUtil.getAppVersionName(activity));
        param.put("osVersion", SystemUtil.getDeviceOSVersion());
        param.put("segment", segment == null ? "" : segment);
        if (chooseServiceExtra != null) {
            param.put("level", String.valueOf(chooseServiceExtra.serviceLevel));
        } else {
            param.put("level", "unknown");
        }
        LogHttpManager.getInstance()
                .getHttpService()
                .saveEvent(param)
                .compose(new CommonTransformer<>())
                .subscribe(httpResult -> {
                }, throwable -> {
                });
    }

    private static String getTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}
